package com.pgeg.enchantment;

import com.pgeg.api.mana.IManaHandler;
import com.pgeg.listener.StatusEffectListener;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.projectile.thrown.PotionEntity;
import net.minecraft.item.Items;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;

public class FireBowEnchantment extends Enchantment {

    public FireBowEnchantment() {
        super(Enchantment.Rarity.VERY_RARE, EnchantmentTarget.BOW, new EquipmentSlot[] {EquipmentSlot.MAINHAND});
    }

    @Override
    public int getMinPower(int level) {
        return 1;
    }

    @Override
    public int getMaxLevel() {
        return 1;
    }

    @Override
    public void onTargetDamaged(LivingEntity user, Entity target, int level) {
        if (((IManaHandler) user).getMana() > 0) {
            if(target instanceof LivingEntity) {
                World world = target.getEntityWorld();
                PotionEntity potionEntity = EntityType.POTION.create(world);
                potionEntity.setItem(Items.FIRE_CHARGE.getDefaultStack());
                potionEntity.setPos(target.getX(), target.getY(), target.getZ());
                world.spawnEntity(potionEntity);
                world.createExplosion(potionEntity, potionEntity.getX(), potionEntity.getY(), potionEntity.getZ(), 1, Explosion.DestructionType.NONE);
                user.addStatusEffect(new StatusEffectInstance(StatusEffectListener.MANA_REDUCTION, 4 * 2 *level, level - 1));
            }
        }

        super.onTargetDamaged(user, target, level);
    }
}
